package com.zrrd.reggie.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zrrd.reggie.bean.Category;
import com.zrrd.reggie.common.R;
import com.zrrd.reggie.service.CategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("category")
public class CategoryController {
    @Autowired
    private CategoryService categoryService;
    @GetMapping("page")
    public R<Page> page(Integer page,Integer pageSize){
        Page<Category> pageInfo=new Page<>(page,pageSize);
        QueryWrapper<Category> wrapper=new QueryWrapper<>();
        wrapper.orderByDesc("sort");
        categoryService.page(pageInfo,wrapper);
        return R.sucess(pageInfo);
    }
    @PostMapping
    public R<String> addCategory(@RequestBody Category category){
        return categoryService.save(category)?R.sucess("添加成功"):R.error("添加失败");
    }
    @PutMapping
    public R<String> updateCategory(@RequestBody Category category){
        return categoryService.updateById(category)?R.sucess("更新成功"):R.error("更新失败");
    }
    @DeleteMapping
    public R<String> deleteCategory(Long id){
        return categoryService.removeById(id)?R.sucess("删除成功"):R.error("删除失败");
    }
    @GetMapping("list")
    @Cacheable(value = "categoryCache")
    public  R<List<Category>> list(Integer type){
        QueryWrapper<Category> wrapper=new QueryWrapper<>();
        if (type!=null){
            wrapper.eq("type",type);
        }
        return R.sucess(categoryService.list(wrapper));
    }
}
